package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiSCC extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  ChoiceGroup saveSaveAs = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
  StringItem note = new StringItem("", "Settings below for Save As only",
                                   StringItem.PLAIN);
  TextField name = new TextField("Name", "", 50, TextField.ANY);
  TextField description = new TextField("Description", "", 50, TextField.ANY);
  TextField creator = new TextField("Creator", "", 50, TextField.ANY);

  public FormOwiSCC(OWIAgent o) {
    super("Save Current Configuration");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(saveSaveAs);
    saveSaveAs.append("Save", null);
    saveSaveAs.append("Save As", null);
    append(note);
    append(name);
    name.setMaxSize(8);
    append(description);
    description.setMaxSize(30);
    append(creator);
    creator.setMaxSize(30);
  }

  public void update() {
    // N/A
  }

  public void save() {

    String msgId = "SCC";
    String[] outputs = {};

    // Setup SET parameters
    if(saveSaveAs.getSelectedIndex() == 1){
      String[] temp = {
                      msgId,
                      "" + saveSaveAs.getSelectedIndex(),
                      name.getString(),
                      description.getString(),
                      creator.getString()
      };

      outputs = temp;

    } else {
      String[] temp = {
                msgId, "" + saveSaveAs.getSelectedIndex()
      };

      outputs = temp;

    }

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this, this, ackCode);

  }

}
